package com.lanou3g.zjl.chating.home;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.TabHost;

import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.home.friends.FriendsFragment;
import com.lanou3g.zjl.chating.home.message.MessageFragment;
import com.lanou3g.zjl.chating.home.setting.SettingFragment;
import com.lanou3g.zjl.chating.utils.BaseActivity;
import com.nineoldandroids.view.ViewHelper;

import butterknife.Bind;

/**
 * Created by dllo on 15/11/17.
 */
public class HomeActivity extends BaseActivity {
    private static final String TAG = "HomeActivity";

//    private DrawerLayout mDrawerLayout;
    @Bind(R.id.mDrawerLayout)
    DrawerLayout mDrawerLayout;
    @Bind(android.R.id.tabhost)
    TabHost tabhost;
    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_home);
        initView();
        initFragment();
        initEvents();
        initData();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_home;
    }

    /**
     * 初始化组件  加载tab页面
     * */
    private void initView() {
//        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
//        tabhost = (TabHost) findViewById(android.R.id.tabhost);
        tabhost.setup();

        TabHost.TabSpec specMessage = tabhost.newTabSpec("message");
        View tabMessage = getLayoutInflater().inflate(R.layout.tab_message_indicator, null);
        specMessage.setIndicator(tabMessage);
        specMessage.setContent(R.id.messageFrm);
        tabhost.addTab(specMessage);

        TabHost.TabSpec specFriends = tabhost.newTabSpec("friends");
        View tabFriends = getLayoutInflater().inflate(R.layout.tab_friends_indicator, null);
        specFriends.setIndicator(tabFriends);
        specFriends.setContent(R.id.friendFrm);
        tabhost.addTab(specFriends);

        TabHost.TabSpec specSetting = tabhost.newTabSpec("setting");
        View tabSetting = getLayoutInflater().inflate(R.layout.tab_setting_indicator, null);
        specSetting.setIndicator(tabSetting);
        specSetting.setContent(R.id.settingFrm);
        tabhost.addTab(specSetting);
    }

    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.replace(R.id.messageFrm, new MessageFragment());
        ft.replace(R.id.friendFrm, new FriendsFragment());
        ft.replace(R.id.settingFrm, new SettingFragment());
        ft.commit();
    }

    @Override
    protected void initData() {

    }

    /**
     * 打开右菜单
     * */
    public void OpenRightMenu(View view) {
        mDrawerLayout.openDrawer(Gravity.RIGHT);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
                Gravity.RIGHT);
    }

    /**
     * 控制抽屉
     * */
    private void initEvents() {
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {

            /**
             * 当抽屉滑动状态改变的时候被调用
             * 状态值是STATE_IDLE（闲置--0）, STATE_DRAGGING（拖拽的--1）, STATE_SETTLING（固定--2）中之一。
             * 抽屉打开的时候，点击抽屉，drawer的状态就会变成STATE_DRAGGING，然后变成STATE_IDLE
             */
            @Override
            public void onDrawerStateChanged(int newState) {

            }

            /**
             * 当抽屉被滑动的时候调用此方法
             * arg1 表示 滑动的幅度（0-1）
             */
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT")) {

                    float leftScale = 1 - 0.3f * scale;

                    ViewHelper.setScaleX(mMenu, leftScale);
                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent,
                            mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent,
                            mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                } else {
                    ViewHelper.setTranslationX(mContent,
                            -mMenu.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
                    ViewHelper.setPivotY(mContent,
                            mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }
            }

            /**
             * 当一个抽屉被完全打开的时候被调用
             */
            @Override
            public void onDrawerOpened(View drawerView) {
            }

            /**
             * 当一个抽屉完全关闭的时候调用此方法
             */
            @Override
            public void onDrawerClosed(View drawerView) {
                mDrawerLayout.setDrawerLockMode(
                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
            }
        });
    }
}

